package com.books.controller.book;


import com.books.model.entity.book.BookType;
import com.books.model.entity.book.Books;
import com.books.service.book.IBookService;
import com.books.service.book.IBookTyoeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired(required = false)
    private IBookTyoeService bookTyoeService;

    @Autowired(required = false)
    private IBookService bookService;

//    @Autowired(required = false)
//    private RedisTemplate redisTemplate;

    @GetMapping("/index")
    public String index(HttpServletRequest request, String name, Integer bookTypeId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "28") Integer pageSize){
        List<BookType> bookTypeList;
        PageInfo<Books> pageInfo;
        bookTypeList = bookTyoeService.findAll();
        if (name!=null&&name.length()>0){
            pageInfo = bookService.index(name,bookTypeId,pageNum,pageSize);
        }else {
            pageInfo = bookService.index(name,bookTypeId,pageNum,pageSize);
//            if(redisTemplate.opsForValue().get("book"+bookTypeId+pageNum+pageSize)==null){
//                pageInfo = bookService.index(name,bookTypeId,pageNum,pageSize);
//                redisTemplate.opsForValue().set("book"+bookTypeId+pageNum+pageSize,pageInfo,1, TimeUnit.DAYS);
//            }
//            else {
//                pageInfo= (PageInfo<Books>) redisTemplate.opsForValue().get("book"+bookTypeId+pageNum+pageSize);
//            }
        }
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("bookTypeList",bookTypeList);
        return "produce/produce_book";
    }

}
